package main

import (
	"fmt"
	"io"
	"log"
	"math/rand"
	"os"
	"runtime"
	"strconv"
)

const count = 10000000 //1e
var cpus = runtime.NumCPU()
var flag  = make(chan*os.File,cpus)
var Current *os.File= nil
//生成大文件
func main(){
	os.MkdirAll("../data/",0644)
	file, err := os.OpenFile("../data/bigData.txt", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
	if err != nil {
		fmt.Errorf("generate data file error2,%v",err)
		return
	}
	defer file.Close()
	for i := 1; i < cpus; i++ {
	go genTemp(i) //开启多个线程写
	}

	for idx := 0;idx < count/cpus;idx++ {
	intn := rand.Intn(100000000)
	file.WriteString(strconv.Itoa(intn)+"\n")
	}
		flag <- Current
	merge(flag,file)
}

func genTemp(idx int) {
	file, err := os.OpenFile("./bigData"+strconv.Itoa(idx)+".txt", os.O_CREATE|os.O_RDWR, 0666)
	defer file.Close()
	if err != nil {
		fmt.Errorf("generate data file error,%v",err)
	}
	for idx := 0;idx<count/cpus;idx++ {
		intn := rand.Intn(1000000)
		file.WriteString(strconv.Itoa(intn)+"\n")
	}
	flag <- file
}

func merge(flag <-chan *os.File,file *os.File){
	len := cap(flag)
	for i := 0; i < len; i++ {
		fileTemp := <-flag
		if fileTemp  == nil{
			continue
		}
		fileTemp, err := os.Open(fileTemp.Name())
		if err != nil {
			log.Fatal("reOpen file %v err%v",fileTemp.Name(),err)
		}
		fmt.Printf("merge file %v\n",fileTemp.Name())
		n, err := io.Copy(file, fileTemp)
		fmt.Println("cppy ",n)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Printf("remove file%v\n",fileTemp.Name())
		fileTemp.Close()
		err = os.Remove(fileTemp.Name())
		if err != nil {
			fmt.Println(err)
		}
	}
	fmt.Printf("generate file done! size:%d",cpus)
}